package com.jgy.cn.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.jgy.cn.mapper.AppUserMapper;
import com.jgy.cn.mapper.DetailLogMapper;
import com.jgy.cn.model.AppUser;
import com.jgy.cn.model.DetailLog;
import com.jgy.cn.service.AppUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jiangxs
 */
@Service
@Slf4j
public class AppUserServiceImpl  implements AppUserService {
    @Autowired
    private AppUserMapper appUserMapper;

    @Autowired
    private DetailLogMapper detailLogMapper;

    @Override
    public String getBalanceById(String userId) {
        AppUser appUser = appUserMapper.selectById(userId);
        if(ObjectUtils.isNotEmpty(appUser)) {
            return "当前用户余额为："+ appUser.getBalance();
        } else {
            return "当前用户不存在";
        }
    }

    @Override
    @Transactional
    public String changeMoney(String userId, String passWord, String money, String type,String name) {
        String message = "成功消费"+money;
        QueryWrapper<AppUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AppUser::getId,userId)
                .eq(AppUser::getPassWord,passWord);
        AppUser appUser = appUserMapper.selectOne(queryWrapper);

        // 校验余额是否充足
        int i = appUser.getBalance().compareTo(Double.parseDouble(money));
        if (i<0) {
            return message = "余额不足，请充值";
        }
        // 记录原余额
        Double balance = appUser.getBalance();
        // 计算剩余金额
        double result = new BigDecimal(Double.toString(balance)).subtract(new BigDecimal(money)).doubleValue();
        appUser.setBalance(result);
        // 变更数据
        appUser.setUpdateDate(new Date());
        int num = appUserMapper.updateById(appUser);
        if (num !=1) {
            return message="数据发生错误,打印用户信息："+ appUser.toString();
        }
        //记录交易明细数据
        DetailLog detailLog = new DetailLog();
        detailLog.setCreateDate(new Date());
        detailLog.setUserId(userId);
        detailLog.setName(name);
        detailLog.setType(type);
        detailLog.setMoney(Double.parseDouble(money));
        detailLog.setTotal(result);
        int insert = detailLogMapper.insert(detailLog);
        log.info("插入详情条数：{}",insert);
        return message;
    }


    /**
     * 退回app
     * @param userId
     * @param money
     * @return
     */
    @Override
    @Transactional
    public String refundByIdAnd1(String userId, String money) {
        String message = "已退回至用户";
        AppUser appUser = appUserMapper.selectById(userId);
        double result = new BigDecimal(Double.toString(appUser.getBalance())).add(new BigDecimal(money)).doubleValue();
        appUser.setBalance(result);
        // 变更数据
        int i = appUserMapper.updateById(appUser);
        if (i !=1) {
            return message="数据发生错误,打印用户信息："+ appUser.toString();
        }
        //记录交易明细数据
        DetailLog detailLog = new DetailLog();
        detailLog.setCreateDate(new Date());
        detailLog.setUserId(userId);
        detailLog.setName("退款");
        detailLog.setType("1");
        detailLog.setMoney(Double.parseDouble(money));
        detailLog.setTotal(result);
        int insert = detailLogMapper.insert(detailLog);
        log.info("插入详情条数：{}",insert);
        return message;
    }

    /**
     * 退回app银行卡
     * @param userId
     * @param money
     * @param cardId
     * @return
     */
    @Override
    @Transactional
    public String refundByIdAnd2(String userId, String money, String cardId) {
        return "调用银行接口";
    }

    @Override
    public List<DetailLog> detailedById(String userId) {
        QueryWrapper<DetailLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(StringUtils.isNotBlank(userId),DetailLog::getUserId,userId);
        queryWrapper.lambda().orderByDesc(DetailLog::getCreateDate);
        List<DetailLog> detailLogs = detailLogMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(detailLogs)) {
            return detailLogs;
        } else {
            return new ArrayList<>();
        }
    }


}
